#!/bin/sh

#参数1：$1 发布的war包名称,如：PDF2PDF
#参数2：$2 发布的war包版本,如：V1.0

TMP_WAR_NAME=$1
TMP_WAR_VERSION=$2

# 定义TOMCAT_HOME环境变量
#if [ -z "$TOMCAT_HOME" ]; then
#  TOMCAT_HOME=/usr/share/tomcat/apache-tomcat-7.0.41
#fi
#export TOMCAT_HOME


if [ -z "$TOMCAT_HOME" ]; then
	TOMCAT_HOME=/usr/share/tomcat/latest
	if [ ! -d "$TOMCAT_HOME" ]
	then
		TOMCAT_HOME=/usr/share/tomcat/apache-tomcat-7.0.37
	fi
fi
export TOMCAT_HOME


WEBAPP_BACKUP_WAR_FULL_PATH="$TOMCAT_HOME/webapps/$TMP_WAR_NAME.war"

echo "==============Backup war=============="
echo $WEBAPP_BACKUP_WAR_FULL_PATH
echo $TMP_WAR_NAME
echo $TMP_WAR_VERSION
echo "============================"

#备份webapp war包到本地文件系统中，用于回滚
WEBAPP_BACKUP_VERSION_DIR="/usr/share/backupFoxitCloud/${TMP_WAR_NAME}/${TMP_WAR_VERSION}"

#/usr/share/backup/PDF2PDF/V1.0
if [ ! -d "$WEBAPP_BACKUP_VERSION_DIR" ];then 
	mkdir -p "$WEBAPP_BACKUP_VERSION_DIR" 
fi

retValue=0
if [ -f "$WEBAPP_BACKUP_WAR_FULL_PATH" ]; then
    cp -rf $WEBAPP_BACKUP_WAR_FULL_PATH $WEBAPP_BACKUP_VERSION_DIR
	retValue=$?
	if [ $retValue != 0 ]; then 
		echo "cp -rf $WEBAPP_BACKUP_WAR_FULL_PATH $WEBAPP_BACKUP_VERSION_DIR error."
	fi
else
	retValue=1
    echo "file($WEBAPP_BACKUP_WAR_FULL_PATH) not exist."
fi

echo "end backupWar:$TMP_WAR_NAME $TMP_WAR_VERSION"
exit $retValue;